package com.zareal.controller;

import com.zareal.domain.Emp;
import com.zareal.dot.GetProductDto;
import com.zareal.dot.PutApplicationDto;
import com.zareal.result.JSONResult;
import com.zareal.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    public IEmpService empService;

    // 查询所有商品
    @RequestMapping(value = "/getproduct/{id}",method = RequestMethod.GET)
    public JSONResult getProduct(@PathVariable("id")Long id){
        JSONResult totalProduct = empService.getProduct(id);
        return JSONResult.success(totalProduct);
    }
    // 查询所有商品
    @RequestMapping(value = "/getproduct",method = RequestMethod.GET)
    public JSONResult getAllProduct(){
        return JSONResult.success(empService.getAllProduct());
    }

    /**
      * 入库
      */
    @RequestMapping(value="/putstorage",method= RequestMethod.POST)
    public JSONResult putStorage(@RequestBody GetProductDto getProductDto){
        JSONResult jsonResult = empService.putStorage(getProductDto);
        return JSONResult.success(jsonResult);
    }
    /**
     * 出库
     */
    @RequestMapping(value="/outstorage",method= RequestMethod.POST)
    public JSONResult outStorage(@RequestBody GetProductDto getProductDto){
        JSONResult jsonResult = empService.outStorage(getProductDto);
        if (jsonResult.getData() == null) {
            return JSONResult.error("出库失败");
        }
        return JSONResult.success(jsonResult.getData());
    }
    /**
     * 查询缺货
     */
    @RequestMapping(value="/outofstock",method= RequestMethod.GET)
    public JSONResult outOfStock(){
        JSONResult jsonResult = empService.outOfStock();
        return JSONResult.success(jsonResult);
    }
    /**
     * 采购
     */
    @RequestMapping(value="/purcharseproduct",method= RequestMethod.POST)
    public JSONResult purcharseProduct(@RequestBody PutApplicationDto putApplicationDto){
        JSONResult jsonResult = empService.putApplication(putApplicationDto);
        return JSONResult.success(jsonResult.getData());
    }
    /**
     * 审批通过
     */
    @RequestMapping(value="/passapplication/{id}",method= RequestMethod.GET)
    public JSONResult passApplication(@PathVariable Long id){
        JSONResult jsonResult = empService.passApplication(id);
        return JSONResult.success(jsonResult.getData());
    }
    /**
     * 审批失败
     */
    @RequestMapping(value="/failapplication/{id}",method= RequestMethod.GET)
    public JSONResult failApplication(@PathVariable Long id){
        JSONResult jsonResult = empService.failApplication(id);
        return JSONResult.success(jsonResult.getData());
    }
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Emp emp){
        if(emp.getId()!=null){
            empService.updateById(emp);
        }else{
            empService.insert(emp);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        empService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(empService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(empService.selectList(null));
    }



}
